#include <sys/select.h>
#include <unistd.h>
#include <cstring>
#include <iostream>

int main()
{
   fd_set readSet;
   FD_ZERO(&readSet);
   char buffer[64]; buffer[0] = 0;
   timeval tv;
   while (true)
   {
      FD_SET(0, &readSet);
      bzero(&tv, sizeof(tv));
      tv.tv_sec = 3;
      int ret = select(1, &readSet, nullptr, nullptr, &tv);
      if (ret < 0) {
         std::cerr << "main::select err..." << std::endl;
      }
      else if (ret == 0) {
         std::cout << "time out..." << std::endl;
      }
      else {
         if (FD_ISSET(0, &readSet)) {
            std::cout << "read ready..." << std::endl;
            ssize_t n = read(0, buffer, 64);
            buffer[n] = 0;
            std::cout << "input: " << buffer;
         }
         else {
            std::cerr << "invalid fd..." << std::endl;
         }
      }
   }
   return 0;
}
